package com.atwisdom.star.core.dao.pojo.functionClass;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.core.dao.pojo.BaseEntity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 
 * @TableName script
 */
@TableName(value ="script",autoResultMap = true)
@Data
public class Script extends BaseEntity {
    /**
     * 事件标识
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 脚本分类
     */
    @TableField(value = "category")
    private String category;

    /**
     * 事件名称
     */
    @TableField(value = "scriptName")
    private String scriptName;

    /**
     * 事件信息
     */
    @TableField(value = "scriptInfo",typeHandler = FastjsonTypeHandler.class)
    private JSONObject scriptInfo;

    /**
     * 版本号
     */
    @TableField(value = "version")
    private BigDecimal version;

    /**
     * 是否发布
     */
    @TableField(value = "published")
    private String published;

    /**
     * 说明
     */
    @TableField(value = "comments")
    private String comments;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;


    public Script(){

    }


    public Script(JSONObject jsonObject) {
        Set<String> keys = jsonObject.keySet();
        this.id = keys.contains("id") ? jsonObject.getLong("id") : null;
        this.category = jsonObject.getString("category");
        this.scriptName = keys.contains("scriptName") ? jsonObject.getString("scriptName") : null;
        this.scriptInfo = keys.contains("scriptInfo") ? jsonObject.getJSONObject("scriptInfo") : null;
        this.comments = keys.contains("comments") ? jsonObject.getString("comments") : null;
        this.version = keys.contains("version") ? jsonObject.getBigDecimal("version") : null;
        this.published = keys.contains("published") ? jsonObject.getString("published") : null;
    }

    /**
     * 获取摘要数据列
     *
     * @return 反回查询列
     */
    public static String[] fetchSimpleColumns() {
        List<String> columns = new ArrayList<>();
        columns.add("id");
        columns.add("category");
        columns.add("scriptName");
        columns.add("scriptInfo");
        columns.add("comments");
        columns.add("updatedBy");
        columns.add("updateTime");
        columns.add("createdBy");
        columns.add("createTime");
        return columns.toArray(new String[0]);
    }

    public JSONObject fetchJsonObject() {
        JSONObject JsonObject = new JSONObject();
        JsonObject.put("id", this.id);
        JsonObject.put("category", this.category);
        JsonObject.put("scriptName", this.scriptName);
        JsonObject.put("scriptInfo", this.scriptInfo);
        JsonObject.put("comments", this.comments);
        JsonObject.put("version", this.version);
        JsonObject.put("published", this.published);
        JsonObject.put("createdBy", this.createdBy);
        JsonObject.put("createTime", this.createTime);
        JsonObject.put("updatedBy", this.updatedBy);
        JsonObject.put("updateTime", this.updateTime);
        return JsonObject;
    }
}